package physics;
import java.util.ArrayList;


public class Polygon extends Shape{
	public ArrayList<Vector> points = new ArrayList<Vector>();
	public ArrayList<Vector> edges = new ArrayList<Vector>();

	public void BuildEdges() {
		Vector p1;
		Vector p2;
		edges = new ArrayList<Vector>();
		for (int i = 0; i < points.size(); i++) {
			p1 = points.get(i);
			if (i + 1 >= points.size()) {
				p2 = points.get(0);
			} else {
				p2 = points.get(i + 1);
			}
			edges.add(p2.add(p1.multiply(-1)));
		}
	}

	public Vector findCenter() {
		float totalX = 0;
		float totalY = 0;
		for (int i = 0; i < points.size(); i++) {
			totalX += points.get(i).x;
			totalY += points.get(i).y;
		}

		return new Vector(totalX / (float)points.size(), totalY / (float)points.size());
	}

	public void Offset(Vector v) {
		Offset(v.x, v.y);
	}

	public void Offset(double x, double y) {
		for (int i = 0; i < points.size(); i++) {
			Vector p = points.get(i);
			points.set(i, new Vector(p.x + x, p.y + y));
		}
	}

	public String ToString() {
		String result = "";

		for (int i = 0; i < points.size(); i++) {
			if (result != "") result += " ";
			result += "{" + points.get(i).toString() + "}";
		}

		return result;
	}

}
